package com.rub.android.service

import com.rub.android.widget.RetrofitServiceApi
import com.rub.android.dao.UserDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext

class UserService private constructor(
    private val userDao: UserDao,
    private val serviceApi: RetrofitServiceApi
) {


    suspend fun refreshLogin(userName:String,passWord:String) = login(userName,passWord)


    private suspend fun login(userName:String,passWord:String) = withContext(Dispatchers.IO) {
        coroutineScope {
            val response = async { serviceApi.login(userName,passWord) }
            var obj = response.await()

            //本地数据
            //userDao.save(tags)
            obj
        }
    }


    companion object {
        private val TAG = javaClass.simpleName

        private var repository: UserService? = null

        fun getInstance(dao: UserDao, api: RetrofitServiceApi): UserService {
            if (repository == null) {
                synchronized(UserService::class.java) {
                    if (repository == null) {
                        repository = UserService(dao, api)
                    }
                }
            }
            return repository!!
        }
    }
}